<template>
  <div id="userLayout" class="user-layout-wrapper">
    <div class="container">
      <div class="user-layout-content">
        <div class="top">
          <h1>警情分析</h1>
          <a-button @click="getMenuInfo">get menu</a-button>
        </div>
        <router-view />
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted } from 'vue';
import { useStore } from 'vuex';
import { SET_INFO, SET_TOKEN } from '@/store/modules/user/mutations';
import { GENERATE_ROUTES } from '@/store/modules/user/actions';

import { useRouter } from 'vue-router';
import { checkedCasApi, userMenu, systemInfo } from '@/api/user/login';
import ls from '@/utils/local-storage';
import { SET_TITLE } from '@/store/modules/app/mutations';
import { routes } from '@/router/index';
import { depthEach } from '@/utils/tree';

export default {
  name: 'UserLayout',
  setup() {
    const store = useStore();
    const router = useRouter();
    const routesMap = {};
    depthEach(routes, item => {
      routesMap[item.name] = item;
    });
    const checkedCas = () => {
      const { auth_token: token } = router.currentRoute.value.query;
      // const { path } = router.currentRoute.value;
      store.dispatch(`user/${GENERATE_ROUTES}`, {}); // 将路由信息存储在vuex中 allowRouters
      return checkedCasApi({ auth_token: token })
        .then(res => {
          store.commit(`user/${SET_TOKEN}`, token);
          store.commit(`user/${SET_INFO}`, {
            userid: res.data.userId,
            name: res.data.userName,
            systemModel: res.data.value.systemModel || 'ZC',
          });
        })
        .catch(err => {
          console.log('err=', err);
        });
    };

    // 获取菜单信息
    const getMenuInfo = () => {
      return userMenu()
        .then(res => {
          const dfs = (treeData, callback) => {
            return treeData.map(item => {
              const map = data => {
                const cData = callback && callback(data);
                if (cData.children) {
                  cData.children = cData.children.map(child => map(child));
                }
                return cData;
              };
              return map(item);
            });
          };
          const newData = dfs(res.data, item => {
            const route = {
              children: item.children,
              name: item.attributes.routeName,
              path: item.attributes.url,
              component: '',
              meta: {
                icon: routesMap[item.attributes.routeName]?.meta?.icon,
                lock: true,
                title: item.name,
              },
            };
            item.attributes.redirect && (route.redirect = item.attributes.redirect);
            return route;
          });
          ls.set('user_menu', newData);
          router.push({ path: '/' });
        })
        .catch(err => {
          console.log('err=', err);
        });
    };
    // 获取系统信息
    const getSystemInfo = () => {
      systemInfo()
        .then(res => {
          const systemName = res.data.name;
          ls.set('systemInfo', {
            systemName,
          });
          store.commit(`app/${SET_TITLE}`, systemName);
        })
        .catch(err => {
          console.log('获取系统信息 err=', err);
        });
    };

    onMounted(async () => {
      await checkedCas();
      getMenuInfo();
      getSystemInfo();
      // redirect
    });

    return {
      getMenuInfo,
    };
  },
};
</script>

<style lang="less" scoped>
#userLayout.user-layout-wrapper {
  height: 100%;
  .container {
    position: relative;
    width: 100%;
    min-height: 100%;
    background: #f0f2f5 url(~@/assets/background.svg) no-repeat 50%;
    background-size: 100%;

    .user-layout-lang {
      width: 100%;
      height: 40px;
      line-height: 44px;
      text-align: right;

      .select-lang-trigger {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        margin-right: 24px;
        padding: 12px;
        font-size: 18px;
        vertical-align: middle;
        cursor: pointer;
      }
    }

    .user-layout-content {
      padding: 32px 0 24px;

      .top {
        text-align: center;

        .header {
          height: 44px;
          line-height: 44px;

          .badge {
            position: absolute;
            display: inline-block;
            margin-top: -10px;
            margin-left: -12px;
            line-height: 1;
            vertical-align: middle;
            opacity: 0.8;
          }

          .logo {
            height: 44px;
            margin-right: 16px;
            vertical-align: top;
            border-style: none;
          }

          .title {
            position: relative;
            top: 2px;
            color: rgba(0, 0, 0, 0.85);
            font-weight: 600;
            font-size: 33px;
            font-family: Avenir, 'Helvetica Neue', Arial, Helvetica, sans-serif;
          }
        }
        .desc {
          margin-top: 12px;
          margin-bottom: 40px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 14px;
        }
      }
      .main {
        width: 368px;
        min-width: 260px;
        margin: 0 auto;
      }
    }
  }
}
</style>
